﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Medianamik.Core.Security;
using System.Web;
using Medianamik.Core;

namespace Medianamik.UI
{
    /// <summary>
    /// Classe représentant une page de Médianamik
    /// </summary>
    public class MedianamikPage : SystemNode<MedianamikPage>
    {
        #region .ctors

        public MedianamikPage()
            : base()
        {

        }

        #endregion

        #region Properties

        #region ISystemNode Members

        public override Guid SystemNodeTypeID
        {
            get { return MedianamikTypes.MedianamikPage; }
        }

        #endregion

		public override string DisplayName
		{
			get
			{
				return (string)HttpContext.GetGlobalResourceObject("SiteMap", FileName);
			}
		}
                

        #region Language Neutral Properties
                    

        public bool IsLanguageNeutral
        {
            get
            {
                return DefaultInstance.Properties["IsLanguageNeutral"].GetValue<bool>();
            }
            set
            {
                DefaultInstance.Properties["IsLanguageNeutral"].SetValue(value);
            }
        }

        public bool DisplayInMenu
        {
            get
            {
                return DefaultInstance.Properties["DisplayInMenu"].GetValue<bool>();
            }
            set
            {
                DefaultInstance.Properties["DisplayInMenu"].SetValue(value);
            }
        }

        public bool IsEditionPage
        {
            get
            {
                return DefaultInstance.Properties["IsEditionPage"].GetValue<bool>();
            }
            set
            {
                DefaultInstance.Properties["IsEditionPage"].SetValue(value);
            }
        }

        public string FileName
        {
            get
            {
                return DefaultInstance.Properties["FileName"].GetValue<string>();
            }
            set
            {
                DefaultInstance.Properties["FileName"].SetValue(value);
            }
        }
        public Guid? ModuleId
        {
            get
            {
                return this.ParentID ;
            }
            set
            {
                this.ParentID = value;
                _module = null;
            }
        }

        private MedianamikModule _module;
        public MedianamikModule Module
        {
            get
            {
                if (!ParentID.HasValue) return null;
                if (_module == null)
                {
                    _module = ConfigManager.Get<MedianamikModule>(ParentID.Value);
                }
                return _module;
            }
        }

		
		public Guid?  MenuGroupId
		{
			get
			{
				return DefaultInstance.Properties["MenuGroup"].GetValue<Guid?>();
			}
			set
			{
				DefaultInstance.Properties["MenuGroup"].SetValue(value);
			}
		}

		public MenuGroup GetMenuGroup()
		{
			return MenuGroupId.HasValue
			? ConfigManager.Get<MenuGroup>(MenuGroupId.Value)
			: default(MenuGroup);
		}

		public IEnumerable<Guid> RoleIds
		{
			get
			{
				return ((ManyToManyNodeProperty)DefaultInstance.Properties["Roles"]).Values;
			}
			set
			{
				DefaultInstance.Properties["Roles"].SetValue(value);
			}
		}
		public RoleNode[] GetRoles()
		{
			return NodeManager.GetNodes<RoleNode>(RoleIds.ToArray())
				.ToArray();
		}


        #endregion

        #endregion

        #region Methods

		//public string GetName(string pCultureName)
		//{
		//    var inst = Instances.Where(i => i.CultureName.Equals(pCultureName, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
		//    if (inst == null) { return null; }

		//    return inst.Properties[NamePropertyId].GetValue<string>();
		//}
		//public void SetName(string pValue, string pCultureName)
		//{
		//    var inst = Instances.Where(i => i.CultureName.Equals(pCultureName, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
		//    if (inst == null) { inst  = AddNodeInstance(pCultureName); }

		//    inst.Properties[NamePropertyId].SetValue(pValue);
		//}

        #endregion
    }
}
